package com.kaifamiao.charstreams;

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.nio.charset.Charset;

/**
 * Writer中的方法
 * 1、abstract void close()
 * 2、void write( int ch )
 * 3、void write( char[] arr )
 * 4、abstract void write( char[] arr, int offset, int length )
 * 5、void write( String s )
 * 6、void write( String s, int offset, int length )
 * 7、Writer append( char ch )
 * 8、Writer append( CharSequence cs )
 * 9、Writer append( CharSequence cs, int start, int end )
 */
public class WriterTest {
    public static void main(String[] args) throws IOException {

        System.out.println( "默认字符编码: " + Charset.defaultCharset() );

        Writer w = new FileWriter( "writer.txt" );

        // 向字符输出流中写入单个字符
        w.write( 65 );
        w.write( 'B' );
        w.write( '\u0043' );
        w.write( '\n' );
        w.write( '开' );
        w.write( '发' );
        w.write( '喵' );
        w.write( '\n' );
        
        char[] chars = { '不', '破', '楼', '兰', '终', '不', '还' };
        // 将整个字符数组中的内容写入到字符输出流
        w.write( chars );
        // 将字符数组中部分字符写入字符输出流中
        w.write( chars, 4, 3 );
        w.write( '\n' );

        String s = "处处蚊子咬";
        // 将整个字字符串写入到字符输出流
        w.write( s );
        w.write( s , 2, 3 );
        w.write( 10 );

        // 将单个字符追加到字符输出流末尾
        w.append( '中' );
        w.append( '国' );

        CharSequence cs = new StringBuilder("人民真争气");
        // 将整个字符序列追加到字符输出流末尾
        w.append( cs );
        // 将字符序列中部分字符追加到字符输出流末尾
        w.append( cs , 2, cs.length() );

        w.close();
    }
}
